package com.yww.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yww.entity.Chat;
import com.yww.mapper.ChatMapper;
import com.yww.service.IChatService;
import com.yww.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 雯雯yang
 * @since 2020-06-14
 */
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    IChatService iChatService;
    @Autowired
    ChatMapper chatMapper;

    /**
     * 添加动态消息
     * @param chat
     * @return
     */
    @RequestMapping("/addMsgContent")
    public Object addMsgContent(Chat chat){
        chat.setSendTime(new Date());
        boolean save = iChatService.save(chat);
        if(save){
            return  new Result(0,"发送成功");
        }
        return  new Result(1,"发送失败");
    }

    /**
     * 咨询师获取动态消息
     * @param userId
     * @return
     */
    @RequestMapping("/getMessage")
    public Object getMessage(Integer userId){
        List<Chat> message = chatMapper.getMessage(userId);
        if(message.size()>0){
            return  new Result(0,"查询成功",message);
        }
        return  new Result(1,"查询失败");
    }


    /**
     * 咨询师获取动态消息
     * @param userId
     * @return
     */
    @RequestMapping("/getMessageRight")
    public Object getMessageRight(Integer userId){
        Chat message = chatMapper.getMessageRight(userId);
        if(message!=null){
            return  new Result(0,"查询成功",message);
        }
        return  new Result(1,"查询失败");
    }


    /**
     * 修改消息读取动态
     * @param chatId
     * @return
     */
    @RequestMapping("/updMessageStatic")
    public Object updMessageStatic(Integer chatId){
        Integer integer = chatMapper.updMessageStatic(chatId);
        if(integer>0){
            return  new Result(0,"修改成功");
        }
        return  new Result(1,"修改失败");
    }


    /**
     * 批量修改消息读取动态
     * @param userId
     * @return
     */
    @RequestMapping("/updBatchMessageStatic")
    public Object updBatchMessageStatic(Integer userId){
        Chat chat=new Chat();
        chat.setConId(userId);
        chat.setMessageStatic(1);//已读
        int con_id = chatMapper.update(chat, new QueryWrapper<Chat>().eq("con_id", userId));
        if(con_id>0){
            return  new Result(0,"修改成功");
        }
        return  new Result(1,"修改失败");
    }


}
